package com.myexam.action;




import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.myexam.model.Student;
import com.myexam.service.StudentService;
import com.myexam.service.SubjectService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class SubjectExamAction extends ActionSupport  {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3782228954082971897L;
	@Autowired
	private StudentService studentService;
	@Autowired
	private SubjectService subjectService;
	private List<Integer> subjectID;
	public List<Integer> getSubjectID() {
		return subjectID;
	}
	public void setSubjectID(List<Integer> subjectID) {
		this.subjectID = subjectID;
	}
	
	public String execute() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		List<String> studentAnswers = new ArrayList<>();
		for(int i = 0;i<20;i++){
			String answer = request.getParameter("subjectAnswer"+i);
			studentAnswers.add(answer);
		}	
		int generalPoint = subjectService.accountResult(subjectID, studentAnswers);
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		Student student = (Student) session.get("studentInfo");
		String exam_id = (String) session.get("exam_id");
		studentService.setStudentResult(student.getStudentID(), generalPoint,Integer.parseInt(exam_id));
		request.setAttribute("studentName", student.getStudentName());
		request.setAttribute("generalPoint", generalPoint);
		session.put("subjectIDs", subjectID);
		session.put("flag"+exam_id, 1);
		session.put("answer", studentAnswers);
		return SUCCESS;
	}
	

}
